﻿using System;
using Coding4Fun.Cannon.Phone.Accelerometer;

namespace Coding4Fun.Cannon.Phone.Cannon
{
	public enum TurningAction
    {
        Left,
        Right,
        None
    }

    public enum TiltingAction
    { 
        Up,
        Down,
        None
    }

    public static class CannonUtility
    {
		const double TurningThreshold = 45;

        public static bool IsTurning(double yAxis)
        {
        	var t = GetTurn(yAxis);
        	return t == TurningAction.Left || t == TurningAction.Right;
        }

    	public static bool IsTilting(double zAxis)
        {
        	var t = GetTilt(zAxis);
        	return t == TiltingAction.Up|| t == TiltingAction.Down;
        }

    	public static TurningAction GetTurn(double yAxis)
        {
            var phoneY = GetExactAngle(yAxis);

            if (Math.Abs(phoneY) <= TurningThreshold)
                return TurningAction.None;

            //Right Turn has negative value
    		return phoneY < 0 ? TurningAction.Right : TurningAction.Left;		
        }

        public static TiltingAction GetTilt(double zAxis)
        {
            var phoneZ = GetExactAngle(zAxis);

            if (Math.Abs(phoneZ) <= TurningThreshold)
                return TiltingAction.None;

            return phoneZ < 0 ? TiltingAction.Down : TiltingAction.Up; 
        }

		public static double GetExactAngle(double value)
		{
			return value * -90;
		}
    }
}
